/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alloy2code;

import alloy2code.Translator;
import alloytoy.AlloyFunctions;
import edu.mit.csail.sdg.alloy4.ConstList;
import edu.mit.csail.sdg.alloy4.SafeList;
import edu.mit.csail.sdg.alloy4compiler.ast.Browsable;
import edu.mit.csail.sdg.alloy4compiler.ast.Decl;
import edu.mit.csail.sdg.alloy4compiler.ast.Expr;
import edu.mit.csail.sdg.alloy4compiler.ast.ExprHasName;
import edu.mit.csail.sdg.alloy4compiler.ast.ExprUnary.Op;
import edu.mit.csail.sdg.alloy4compiler.ast.Func;
import edu.mit.csail.sdg.alloy4compiler.ast.Module;
import edu.mit.csail.sdg.alloy4compiler.ast.Sig;
import java.io.IOException;
import java.util.List;
/**
 *
 * @author Daniel
 */
public class Alloy2Code {
    Translator trans;

    //If str has the form of "this/something", returns something.
    public String removeThis(String str){
        if(str.indexOf("this/")!=-1){
            return str.substring(5);
        }
        return str;
        
    }
    public Alloy2Code(String moduleName, Translator t) throws IOException{
        trans = t;
        Translate(trans);
        AlloyFunctions model = new AlloyFunctions();
        model.initAll(moduleName);
        Module world = model.getModule();
        SafeList<Sig> sigs = world.getAllSigs();
        
        for(int i=0; i<sigs.size(); i++){
            SigData sigData = new SigData();
            sigData.setSigName(removeThis(sigs.get(i).label));
            SafeList<Decl> decls = sigs.get(i).getFieldDecls();
            for(int j=0; j<decls.size(); j++){
                Declaration declTmp = new Declaration();
                Expr e = decls.get(j).expr;
                String name = removeThis(decls.get(j).get().label);
                declTmp.setLabel(name);
                if(e.mult==1){
                    Op o = e.mult();
                    if(o.equals(o.SETOF)) declTmp.setMult("set");
                    if(o.equals(o.ONEOF)) declTmp.setMult("one");
                    if(o.equals(o.SOMEOF)) declTmp.setMult("some");
                    if(o.equals(o.LONEOF)) declTmp.setMult("lone");
                    //System.out.println(name);
                    List<Browsable> nodes = (List<Browsable>) e.getSubnodes();
                    declTmp.setSigName(removeThis(nodes.get(0).toString()));
                    sigData.addDecl(declTmp);
                }
            }
            System.out.println(t.SigTranslate(sigData));
        }
        /*SafeList<Func> funcs = world.getAllFunc();
        for(int i=0; i<funcs.size(); i++){
            /System.out.println(trans.PredTranslate(funcs.get(i)));
        }*/
    }

    public static void Translate(Translator t){
    }
}